﻿package mortal.game.view.monthInvestment
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.gengine.global.*;
    import com.mui.controls.*;
    import extend.language.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.utils.*;
    import mortal.game.view.investmentProject.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class MonthInvestmentController extends Controller
    {
        private var _module:MonthInvestmentModule;

        public function MonthInvestmentController()
        {
            return;
        }// end function

        override protected function initView() : IView
        {
            if (this._module == null)
            {
                this._module = new MonthInvestmentModule();
                this._module.addEventListener(WindowEvent.SHOW, this.onShow);
                this._module.addEventListener(WindowEvent.CLOSE, this.onClose);
            }
            return this._module;
        }// end function

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.InvestmentInfoUpdate, this.updateInvestment);
            Dispatcher.addEventListener(EventName.MonthInvestmentOpen, this.openWin);
            Cache.instance.role.roleEntityInfo.addEventListener(EntityInfoEventName.UpdateLevel, this.updateInvestment);
            return;
        }// end function

        private function onShow(event:WindowEvent) : void
        {
            Dispatcher.addEventListener(EventName.InvestmentMonth, this.investmen);
            return;
        }// end function

        private function onClose(event:WindowEvent) : void
        {
            Dispatcher.removeEventListener(EventName.InvestmentMonth, this.investmen);
            return;
        }// end function

        private function investmen(event:DataEvent) : void
        {
            var type:int;
            var money:int;
            var closeAlert:Function;
            var e:* = event;
            closeAlert = function (param1:int) : void
            {
                if (param1 == Alert.OK)
                {
                    if (Cache.instance.role.enoughMoney(EPriceUnit._EPriceUnitGold, money))
                    {
                        GameProxy.player.investment(type);
                    }
                    else
                    {
                        MsgManager.showRollTipsMsg(Language.getString(35108), MsgRollTipsType.msgRollTips3);
                    }
                }
                return;
            }// end function
            ;
            type = e.data as int;
            var info:* = MonthInvestmentConfig.instance.getInvestBaseByType(type);
            if (info)
            {
                money = info.cost;
            }
            Alert.show(Language.getStringByParam(35409, money), null, Alert.OK | Alert.CANCEL, null, closeAlert, null, 4);
            return;
        }// end function

        private function openWin(event:DataEvent) : void
        {
            if (!this.isViewShow)
            {
                view.show();
            }
            else
            {
                view.hide();
            }
            return;
        }// end function

        private function updateInvestment(param1:Object) : void
        {
            var _loc_2:int = 0;
            if (param1 is int)
            {
                _loc_2 = param1 as int;
                if (_loc_2 == EInvestType._EInvestTypeResigst)
                {
                    return;
                }
            }
            if (Global.isDebugModle || Cache.instance.role.entityInfo.level >= GameDefConfig.instance.getSystemOpenLevel(88) && ClockManager.instance.getLoginDayNumByPassZero() > 7)
            {
                MonthInvestmentIcon.instance.show();
                this.isShowEffect();
                if (this.isViewShow)
                {
                    this._module.updateTab();
                    this._module.updateInvestMent();
                }
            }
            else
            {
                MonthInvestmentIcon.instance.hide();
            }
            return;
        }// end function

        private function isShowEffect(param1:Object = null) : void
        {
            if (Cache.instance.role.entityInfo.level >= GameDefConfig.instance.getSystemOpenLevel(21))
            {
                if (InvestUtil.isMonthInvestCanGet)
                {
                    MonthInvestmentIcon.instance.playEffect();
                }
                else
                {
                    MonthInvestmentIcon.instance.stopEffect();
                }
                NetDispatcher.removeCmdListener(ServerCommand.RoleLevelUpdate, this.isShowEffect);
            }
            else
            {
                InvestmentIcon.instance.stopEffect();
            }
            return;
        }// end function

    }
}
